package com.feng.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.feng.entity.BookType;
import com.feng.util.db.DBHelp;
import com.feng.util.db.Page;
import com.feng.util.db.RowMapper;

public class BookTypeDao {

	DBHelp<BookType> db = new DBHelp<BookType>();

	public int getTotle() {
		String sql = "select count(*) from t_booktype order by createtime desc";
		return  (int) db.executeQuery(sql);
	}
	
	private class BookTypeRowMapper implements RowMapper<BookType>{

		public BookType mapRow(ResultSet rs) throws SQLException {
			BookType b = new BookType();
			b.setId(rs.getString("id"));
			b.setTypeName(rs.getString("typename"));
			b.setCreateTime(rs.getString("createtime"));
			return b;
		}
		
	}
	public Page<BookType> findByPageNum(int pageNum) {
		Page<BookType> page = new Page<BookType>(getTotle(),pageNum);
		String sql = "select id,typename,createtime from t_booktype order by createtime desc limit ?,?";
		List<BookType> list = db.executeQueryToList(sql, new BookTypeRowMapper(),(pageNum-1)*page.getPageSize(),page.getPageSize());
		page.setList(list);
		return page;
	}
	public BookType findById(String id) {
		String sql = "select id,typename,createtime from t_booktype where id=? order by createtime desc";
		return db.executeQueryToObject(sql, new BookTypeRowMapper(), id);
	}
	public void save(String id, String type) {
		String sql = "update t_booktype set typename=? where id=?";
		db.executeSQL(sql, type,id);
	}
	public void create(String id, String type) {
		String sql = "insert into t_booktype(id,typename) values(?,?) ";
		db.executeSQL(sql, id,type);
	}
	public void del(String id) {
		String sql = "delete from t_booktype where id=?";
		db.executeSQL(sql, id);
	}
	public List<BookType> findAll() {
		String sql = "select id,typename,createtime from t_booktype";
		return db.executeQueryToList(sql, new BookTypeRowMapper());
	}
}
